package main

import (
	"fmt"
	"reflect"

	"github.com/ahviplc/GoJustToolc/UConsole"

	"github.com/spf13/viper"
)

func main() {
	// 读取json文件
	v := viper.New()
	// 设置读取的配置文件名
	v.SetConfigName("config") // config.json 所以填写为config
	// 设置配置文件类型
	v.SetConfigType("json")
	// windows环境下为%GOPATH，linux环境下为$GOPATH
	// config下
	v.AddConfigPath("config") // 均可以: config   /config   ./config/   ./config/.

	if err := v.ReadInConfig(); err != nil {
		fmt.Printf("err:%s\n", err)
	}

	// 打印
	UConsole.PrintAStraightLine()
	fmt.Println(v.AllKeys())
	// fmt.Println(v.AllowEmptyEnv) // 0x7f2800
	UConsole.PrintAStraightLine()
	//fmt.Printf("GoToolcUrl:%s author:%s \n", v.Get("GoToolcUrl"), v.Get("author"))
	for k, v := range v.AllKeys() {
		fmt.Println(k, " -> ", v)
	}
	UConsole.PrintAStraightLine()
	fmt.Println(v.AllSettings())
	UConsole.PrintAStraightLine()
	for k, v := range v.AllSettings() {
		fmt.Println(k, " -> ", v)
	}
	UConsole.PrintAStraightLine()
	UConsole.PrintAStraightLine()
	allSettingsMap := v.AllSettings()
	UConsole.Log("循环遍历key为contents的子Map,结果如下:")
	// 使用【.(map[string]interface{})】将【allSettingsMap["contents"]】强制转成map
	// 使用类型断言 if contentsMap, ok := allSettingsMap["contents"].(map[int]interface{})
	// 如果 allSettingsMap["contents"]是(map[int]interface{})类型的话,ok就是true,contentsMap就是(map[int]interface{})类型allSettingsMap["contents"]的值.
	// 否则ok为false，contentsMap就是(map[int]interface{})类型的初始化 nil
	if contentsMap, ok := allSettingsMap["contents"].(map[string]interface{}); ok {
		UConsole.Log("类型转换成功:", ok)
		for k, v := range contentsMap {
			fmt.Println(k, " -> ", v)
		}
	} else {
		UConsole.Log("无效的类型转换:", ok) // 如果无效类型转换 则 contentsMap 会是初始值 nil
	}
	UConsole.PrintAStraightLine()
	UConsole.PrintTypeAndValue(allSettingsMap["contents2"])
	fmt.Println(reflect.TypeOf(allSettingsMap["contents2"])) // []interface {}
	c2 := allSettingsMap["contents2"]
	for k, v := range c2.([]interface{}) {
		fmt.Println(k, " -> ", v)
	}
	UConsole.PrintAStraightLine()
	if contents2Map, ok := allSettingsMap["contents2"].([]interface{}); ok {
		UConsole.Log("类型转换成功:", ok)
		for k, v := range contents2Map {
			fmt.Println(k, " -> ", v)
		}
	} else {
		UConsole.Log("无效的类型转换:", ok) // 如果无效类型转换 则 contents2Map 会是初始值 nil
	}
	UConsole.PrintAStraightLine()
	if contents2Map, ok := allSettingsMap["contents2"].([]interface{})[0].(map[string]interface{}); ok {
		UConsole.Log("类型转换成功:", ok)
		UConsole.PrintTypeAndValue(contents2Map) // 类型(Type):map[string]interface {}  值(Value):map[post1:3 post2:4]
		for k, v := range contents2Map {
			fmt.Println(k, " -> ", v)
		}
	} else {
		UConsole.Log("无效的类型转换:", ok) // 如果无效类型转换 则 contents2Map 会是初始值 nil
	}
	UConsole.PrintAStraightLine()

	// 判断key是否存在
	keyTemp := "contents3" // 存在
	if i, ok := allSettingsMap[keyTemp]; ok {
		UConsole.Log(keyTemp, "-key-存在")
		UConsole.PrintTypeAndValue(i)
	} else {
		UConsole.Log(keyTemp, "-key-不存在")
	}

	// -----------------------------------------------------------------------------------------------------
	//	[message projectname version gotoolcurl nowdatetime usegotoolc author]
	// -----------------------------------------------------------------------------------------------------
	// 0  ->  nowdatetime
	// 1  ->  usegotoolc
	// 2  ->  author
	// 3  ->  message
	// 4  ->  projectname
	// 5  ->  version
	// 6  ->  gotoolcurl
	// -----------------------------------------------------------------------------------------------------
	// map[author:LC<ahlc@sina.cn> gotoolcurl:https://github.com/ahviplc/GoJustToolc message:I am root nowdatetime:2020-07-05 13:53:17 projectname:mygindemo usegotoolc:df19b321bba50602ee2539f72788d60a version:v0.1.0]
	// -----------------------------------------------------------------------------------------------------
	// gotoolcurl  ->  https://github.com/ahviplc/GoJustToolc
	// nowdatetime  ->  2020-07-05 13:53:17
	// usegotoolc  ->  df19b321bba50602ee2539f72788d60a
	// author  ->  LC<ahlc@sina.cn>
	// message  ->  I am root
	// projectname  ->  mygindemo
	// version  ->  v0.1.0
	// -----------------------------------------------------------------------------------------------------
	// 循环遍历key为contents的子Map,结果如下:
	// 类型转换成功:true
	// post2  ->  2
	// post1  ->  1
	// -----------------------------------------------------------------------------------------------------
	// contents2-key-存在
	// 类型(Type):[]interface {}  值(Value):[map[post1:3 post2:4]]
	// -----------------------------------------------------------------------------------------------------
	// contents3-key-不存在
}
